package com.wyp168.juc.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName atomicIntegerDemo001
 * @Description TODO
 * @Author wyp168
 * @Date 2022/9/18 12:32
 */
public class atomicIntegerDemo001 {
    public static final int SIZE = 50;
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger(1);
        CountDownLatch countDownLatch = new CountDownLatch(SIZE);
        for (int i = 0; i < SIZE; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < 1000; j++) {
                        atomicInteger.incrementAndGet();
                    }
                } finally {
                    countDownLatch.countDown();
                }

            }, "t" + i).start();
        }
        countDownLatch.await();
//        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) {e.printStackTrace();}
        System.out.println(Thread.currentThread().getName() + "\t result:" + atomicInteger.get());
    }
}
